package edu.ped.android.view;

import edu.ped.android.nfc.TicTacToeServerActivity;
import edu.ped.android.utils.TagsMatrice;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

public class TicTacToeView extends View {

	private static final int MATRICE_HEIGHT=3;
	private static final int MATRICE_WIDTH=3;
	private static final int CIRCLE_RADIUS = 30;

	TagsMatrice matrice;
	Paint paint;

	public TicTacToeView(Context context, AttributeSet attrs) {
		super(context, attrs);
		paint = new Paint();
		paint.setColor(Color.BLACK);
		paint.setStyle(Paint.Style.STROKE);
		setBackgroundColor(Color.WHITE);
	}

	public void setMatrice(TagsMatrice matrice){
		this.matrice = matrice;
	}

	@Override
	protected void onDraw(Canvas canvas) {
		Log.i("PED","OnDraw");
		int tailleX=  (getWidth() /  MATRICE_WIDTH);
		int tailleY=  (getHeight() /   MATRICE_HEIGHT);
		
		//dessin de la grille
		//verticales
		canvas.drawLine(getWidth()/3, 0, getWidth()/3, getHeight(), paint);
		canvas.drawLine(getWidth()*2/3, 0, getWidth()*2/3, getHeight(), paint);
		//horizontales
		canvas.drawLine(0, getHeight()/3, getWidth(), getHeight()/3, paint);
		canvas.drawLine(0, getHeight()*2/3, getWidth(), getHeight()*2/3, paint);
		
		if(matrice!=null){
			for(int i=0;i<MATRICE_WIDTH;i++)
				for(int j=0;j<MATRICE_HEIGHT;j++){
					int sign = ((Integer)matrice.getTag(i, j).getValeur()).intValue();
					float centerX =j*tailleX+tailleX/2;
					float centerY = i*tailleY+tailleY/2;
					if(sign == TicTacToeServerActivity.CIRCLE_SIGN)
						canvas.drawCircle(centerX, centerY, CIRCLE_RADIUS, paint);
					else if(sign == TicTacToeServerActivity.CROSS_SIGN){
						canvas.drawLine(centerX, centerY, centerX-CIRCLE_RADIUS, centerY-CIRCLE_RADIUS, paint);
						canvas.drawLine(centerX, centerY, centerX-CIRCLE_RADIUS, centerY+CIRCLE_RADIUS, paint);
						canvas.drawLine(centerX, centerY, centerX+CIRCLE_RADIUS, centerY-CIRCLE_RADIUS, paint);
						canvas.drawLine(centerX, centerY, centerX+CIRCLE_RADIUS, centerY+CIRCLE_RADIUS, paint);
					}
				}
		}
	}
}